<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="支付平台" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付平台" size="mini" clearable style="width: 140px">
          <el-option :value="0" label="微信支付"/>
          <el-option :value="1" label="银联微信"/>
        </el-select>
      </el-form-item>
      <el-form-item label="来源平台" prop="platform">
        <el-select v-model="queryParams.platform" placeholder="请选择来源平台" size="mini" clearable style="width: 140px">
          <el-option :value="0" label="微信小程序"/>
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="选择订单状态" clearable style="width:140px;" size="mini">
          <el-option label="未支付" :value="0" />
          <el-option label="待支付" :value="1" />
          <el-option label="已支付" :value="2" />
          <el-option label="已核销" :value="3" />
          <el-option label="已评价" :value="4" />
          <el-option label="订单取消" :value="5" />
          <el-option label="未核销" :value="10" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="查询时间" prop="propertyName">
        <el-date-picker v-model="dateRange" clearable size="mini" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"-->
      <!--          v-hasPermi="['module:productorder:edit']">修改</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['module:productorder:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
                   v-hasPermi="['module:productorder:export']">导出</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="订单号" align="center" prop="orderNo" width="160"/>
      <!--      <el-table-column label="所属物业" align="center" prop="propertyName" width="130"/>-->
      <el-table-column label="所属小区" align="center" prop="estateName" width="120"/>
      <el-table-column label="所属商家" align="center" prop="shopName" width="180"/>
      <el-table-column label="用户姓名" align="center" prop="realName" width="150"/>
<!--      <el-table-column label="来自平台" align="center" prop="platform" width="110">-->
<!--        <template slot-scope="scope">-->
<!--          <el-tag v-if="scope.row.platform === 0" style="cursor: pointer" :type="''">微信小程序</el-tag>-->
<!--          <el-tag v-if="scope.row.platform === 1" style="cursor: pointer" :type="'success'">抖音小程序</el-tag>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <!--      <el-table-column label="详细地址" align="center" prop="userAddress" />-->
      <el-table-column label="订单总价" align="center" prop="totalPrice" width="120">
        <template slot-scope="scope">{{scope.row.totalPrice}}元</template>
      </el-table-column>
      <el-table-column label="支付金额" align="center" width="130">
        <template slot-scope="scope">{{scope.row.payPrice}}元</template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createTime" width="160"/>
      <el-table-column label="支付时间" align="center" prop="payTime" width="160"/>
      <el-table-column label="用户电话" align="center" prop="userPhone" width="110"/>
      <el-table-column label="核销码" align="center" prop="verifyCode" width="130"/>


      <el-table-column label="订单类型" align="center" prop="orderType" width="120">
        <template slot-scope="scope">
          <!--          //订单类型  0 - 平台商品类  1 - 设备上的商品类  2 - 线下商家活动  3 - 商家优惠券 4 - 团购类-->
          <el-tag v-if="scope.row.orderType === 0" style="cursor: pointer" :type="'success'">平台商品类</el-tag>
          <el-tag v-if="scope.row.orderType === 1" style="cursor: pointer" :type="'success'">设备订单</el-tag>
          <el-tag v-if="scope.row.orderType === 2" style="cursor: pointer" :type="'success'">线下商家活动</el-tag>
          <el-tag v-if="scope.row.orderType === 3" style="cursor: pointer" :type="'success'">商家优惠券</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="结算状态" align="center" prop="orderType" width="110" fixed="right">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.settlementState === 0" style="cursor: pointer" :type="'danger'">未结算</el-tag>
          <el-tag v-if="scope.row.settlementState === 1" style="cursor: pointer" :type="'success'">已结算</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="status" fixed="right">
        <template slot-scope="scope">
          <div>
            <!-- 订单状态 0 - 未支付   1 - 待支付  2 - 已支付  3 - 已核销 4 - 已评价
            5 - 订单取消  6 - 已申请退货  7 - 同意退货  8 - 退货完成 9 - 拒绝退货 10 - 已完成 -->
            <el-tag v-if="scope.row.status === 0" style="cursor: pointer" :type="'danger'">未支付</el-tag>
            <el-tag v-if="scope.row.status === 1" style="cursor: pointer" :type="''">待支付</el-tag>
            <el-tag v-if="scope.row.status === 2" style="cursor: pointer" :type="'success'">已支付</el-tag>
            <el-tag v-if="scope.row.status === 3" style="cursor: pointer" :type="'success'">已核销</el-tag>
            <el-tag v-if="scope.row.status === 4" style="cursor: pointer" :type="'success'">已评价</el-tag>
            <el-tag v-if="scope.row.status === 5" style="cursor: pointer" :type="'warning'">订单取消</el-tag>
            <el-tag v-if="scope.row.status === 6" style="cursor: pointer" :type="'danger'">已申请退货</el-tag>
            <el-tag v-if="scope.row.status === 7" style="cursor: pointer" :type="'danger'">同意退货</el-tag>
            <el-tag v-if="scope.row.status === 8" style="cursor: pointer" :type="'danger'">退货完成</el-tag>
            <el-tag v-if="scope.row.status === 9" style="cursor: pointer" :type="'danger'">拒绝退货</el-tag>
            <el-tag v-if="scope.row.status === 10" style="cursor: pointer" :type="'success'">已完成</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-s-goods">
            <router-link :to="'/orders/info?orderId=' + scope.row.id+'&from=order'" class="link-type">
              详情
            </router-link>
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete"  v-if="scope.row.status !==2 && scope.row.status !== 3"
                     @click="handleDelete(scope.row)" v-hasPermi="['module:productorder:remove']">删除</el-button>
<!--          <el-button v-if="scope.row.status === 0 || scope.row.status === 1" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"-->
<!--                     v-hasPermi="['module:productorder:edit']">改价</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="订单总价" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入订单总价" size="mini">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="支付金额" prop="payPrice">
          <el-input v-model="form.payPrice" placeholder="请输入支付金额" size="mini">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-radio-group v-model="form.state">
            <el-radio :value="0" :label="0">未支付</el-radio>
            <el-radio :value="1" :label="1">待支付</el-radio>
            <el-radio :value="2" :label="2">已支付</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder,exportOrderTotal } from "@/api/module/product/order";
  import Template from "../wx/template";

  export default {
    name: "Order",
    components: {Template},
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        dateRange:[],
        // 总条数
        total: 0,
        // 订单表格数据
        orderList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          status:undefined,
          merId:undefined,
          orderNo: undefined,
          beginDate:undefined,
          endDate:undefined,
          orderType:0,
          platform:undefined,
          payType:undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      // this.queryParams.status = 3;
      this.queryParams.merId = this.$route.query.merId?this.$route.query.merId:undefined;
      this.getList();
    },
    methods: {
      /** 查询订单列表 */
      getList() {
        this.loading = true;
        listOrder(this.queryParams).then(response => {
          this.orderList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          orderNo: undefined,
          outTradeNo: undefined,
          uid: undefined,
          realName: undefined,
          userPhone: undefined,
          userAddress: undefined,
          cartId: undefined,
          freightPrice: undefined,
          totalNum: undefined,
          totalPrice: undefined,
          totalPostage: undefined,
          payPrice: undefined,
          payPostage: undefined,
          deductionPrice: undefined,
          couponId: undefined,
          couponPrice: undefined,
          paid: undefined,
          payTime: undefined,
          payType: undefined,
          createTime: undefined,
          updateTime: undefined,
          status: 0,
          refundStatus: 0,
          refundReasonWapImg: undefined,
          refundReasonWapExplain: undefined,
          refundReasonTime: undefined,
          refundReasonWap: undefined,
          refundReason: undefined,
          refundPrice: undefined,
          deliverySn: undefined,
          deliveryName: undefined,
          deliveryType: undefined,
          deliveryId: undefined,
          gainIntegral: undefined,
          useIntegral: undefined,
          backIntegral: undefined,
          mark: undefined,
          isDel: undefined,
          unique: undefined,
          remark: undefined,
          merId: undefined,
          isMerCheck: undefined,
          combinationId: undefined,
          cost: undefined,
          bargainId: undefined,
          verifyCode: undefined,
          storeId: undefined,
          shippingType: undefined,
          isChannel: undefined,
          isRemind: undefined,
          isSystemDel: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        console.log('dateRange',this.dateRange);
        if(this.dateRange !=undefined && this.dateRange.length === 2){
          this.queryParams.beginDate = this.dateRange[0];
          this.queryParams.endDate = this.dateRange[1];
        }
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length!==1;
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加订单";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids;
        getOrder(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改订单";
        });
      },
      removeImg(data){
        console.log('removeImg',data);
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateOrder(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addOrder(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除订单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delOrder(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
</script>
